﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Web;
using System.Text;
using Api.Common;

namespace MyWebApi.Controllers
{
    public class WfInstanceController : ApiController
    {
        Api.Models.wf_instance model = new Api.Models.wf_instance();
        Api.BLL.wf_instance bll = new Api.BLL.wf_instance();
        Api.BLL.manager manager_bll = new Api.BLL.manager();
        DataSet ds = new DataSet();

        [HttpGet,HttpPost]
        private bool ExitsUser(string _token)
        {
            if (string.IsNullOrEmpty(_token.ToString()))
                return false;
            if (_token.Length < 5)
                return false;
            if (_token.IndexOf("==") == -1)
                return false;
            if (!manager_bll.Exists(Utils.Base64Decode(_token)))
                return false;
            return true;
        }

        #region 首页待办列表
        public IHttpActionResult GetWfInstanceList(string userid)
        {
            if (!ExitsUser(userid))
                return NotFound();
            else
            {
                int id = manager_bll.GetModel(Utils.Base64Decode(userid)).id;

                DataTable dt = new DataTable();
                dt.Columns.Add("id", Type.GetType("System.Int32"));
                dt.Columns.Add("stepId", Type.GetType("System.Int32"));
                dt.Columns.Add("Wf_temp", Type.GetType("System.String"));
                dt.Columns.Add("InstName", Type.GetType("System.String"));
                dt.Columns.Add("StepName", Type.GetType("System.String"));
                dt.Columns.Add("Checktime", Type.GetType("System.String"));
                dt.Columns.Add("processby", Type.GetType("System.String"));
                dt.Columns.Add("StepAnotherName", Type.GetType("System.String"));

                //从步骤表中查询当前登陆用户所接收的步骤 状态为未审核
                Api.BLL.wf_step stepbll = new Api.BLL.wf_step();
                string strwhere = " ProcessBy=" + id + " and Status=" + (short)EazyEnums.WFStepStatusEnum.Running + " order by Checktime desc";
                DataTable OldDT = stepbll.GetList(strwhere).Tables[0];

                if (OldDT.Rows.Count > 0)
                {
                    for (int i = 0; i < OldDT.Rows.Count; i++)
                    {
                        Api.BLL.wf_instance bll = new Api.BLL.wf_instance();
                        string strWhere = " id=" + Convert.ToInt32(OldDT.Rows[i]["Instance_id"]) + " order by SubTime desc";
                        DataTable NewDt = bll.GetList(strWhere).Tables[0];
                        if (NewDt.Rows.Count > 0)
                        {
                            for (int j = 0; j < NewDt.Rows.Count; j++)
                            {
                                DataRow tr = dt.NewRow();
                                tr["id"] = NewDt.Rows[j]["id"].ToString();
                                tr["stepId"] = OldDT.Rows[j]["id"];
                                if (NewDt.Rows[j]["WF_TempID"].ToString() == "7")
                                    tr["Wf_temp"] = "举报投诉";
                                else if (NewDt.Rows[j]["WF_TempID"].ToString() == "8")
                                    tr["Wf_temp"] = "一般程序案件";
                                tr["InstName"] = NewDt.Rows[j]["InstName"];
                                tr["StepName"] = OldDT.Rows[j]["StepName"];
                                tr["Checktime"] = OldDT.Rows[j]["Checktime"];
                                tr["processby"] = manager_bll.GetModel(Convert.ToInt32(OldDT.Rows[j]["ProcessBy"])).real_name;
                                tr["StepAnotherName"] = OldDT.Rows[i]["StepAnotherName"].ToString();

                                dt.Rows.Add(tr);
                            }
                        }
                    }
                }
                return Ok(dt);
            }
        }
        #endregion

        #region 一般案件待办列表
        [HttpGet,HttpPost]
        public IHttpActionResult GetWaitWorkList(string userid)
        {
            if (!ExitsUser(userid))
                return NotFound();
            else
            {
                int id = manager_bll.GetModel(Utils.Base64Decode(userid)).id;
                //从步骤表中查询当前登陆用户所接收的步骤 状态为未审核
                Api.BLL.wf_step stepbll = new Api.BLL.wf_step();
                Api.BLL.wf_instance bll = new Api.BLL.wf_instance();

                string strwhere = " ProcessBy=" + id + " and Status=" + (short)EazyEnums.WFStepStatusEnum.Running + " order by Checktime desc";
                DataTable dt = stepbll.GetList(strwhere).Tables[0];

                //表格重组
                DataTable dtN = new DataTable();
                dtN.Columns.Add("ID", Type.GetType("System.Int32"));
                dtN.Columns.Add("InstName");
                dtN.Columns.Add("SubBy", Type.GetType("System.Int32"));
                dtN.Columns.Add("SubTime");
                dtN.Columns.Add("EndTime");
                dtN.Columns.Add("Status", Type.GetType("System.Int32"));
                dtN.Columns.Add("StepId", Type.GetType("System.Int32"));
                dtN.Columns.Add("StepAnotherName");
                dtN.Columns.Add("Isback", Type.GetType("System.Int32"));
                dtN.Columns.Add("value", Type.GetType("System.Int32"));

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataTable NewDt = bll.GetList(" WF_TempID=8 and id=" + Convert.ToInt32(dt.Rows[i]["Instance_id"]) + " order by SubTime desc").Tables[0];
                    if (NewDt != null)
                    {
                        for (int j = 0; j < NewDt.Rows.Count; j++)
                        {
                            if (Convert.ToInt32(NewDt.Rows[j]["Status"]) != 2 && Convert.ToInt32(NewDt.Rows[j]["Status"]) != 3)
                            {
                                DataRow tr = dtN.NewRow();
                                tr["ID"] = NewDt.Rows[j]["ID"].ToString();
                                tr["InstName"] = NewDt.Rows[j]["InstName"];
                                tr["SubBy"] = NewDt.Rows[j]["SubBy"];
                                tr["SubTime"] = NewDt.Rows[j]["SubTime"];
                                tr["EndTime"] = NewDt.Rows[j]["EndTime"];
                                tr["Status"] = NewDt.Rows[j]["Status"];
                                tr["StepId"] = dt.Rows[i]["ID"];
                                tr["Isback"] = dt.Rows[i]["Isback"];
                                tr["value"] = dt.Rows[i]["value"];
                                tr["StepAnotherName"] = dt.Rows[i]["StepAnotherName"];

                                dtN.Rows.Add(tr);
                            }
                        }
                    }
                }
                return Ok(dtN);
            }
        }
        #endregion
    }
}
